package com.example.android2019.ch2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.android2019.R;

public class Ch2_marketActivity extends AppCompatActivity {
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ch2_market);
        listView = findViewById(R.id.ch2_market_item_list);
         final MyAdapter adapter = new MyAdapter();
        listView.setAdapter(adapter);
        AdapterView.OnItemClickListener listener = new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String s = adapter.names[position];
                Toast.makeText(Ch2_marketActivity.this, s, Toast.LENGTH_SHORT).show();
            }
        };
        listView.setOnItemClickListener(listener);
    }

    class MyAdapter extends BaseAdapter {
        int[] img = {R.drawable.jd, R.drawable.qq, R.drawable.dz, R.drawable.xl, R.drawable.tm, R.drawable.uc, R.drawable.wx};
        String[] names = {"京东商城", "QQ", "QQ斗地主", "新浪微博", "天猫商城", "UC浏览器", "微信"};

        @Override

        public int getCount() {
            return names.length;
        }

        @Override
        public Object getItem(int position) {
            return names[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
     /*       View view = View.inflate(Ch2_marketActivity.this, R.layout.activity_ch2_market_item, null);
            ImageView imageView = view.findViewById(R.id.ch2_market_item_image);
            TextView textView = view.findViewById(R.id.ch2_market_tv_item);
            imageView.setImageResource(img[position]);
            textView.setText(names[position]);
            return view;*/


            /*数据量过大时，为了优化ListView减少耗时操作，利用ViewHolder类*/
            ViewHolder viewHolder = new ViewHolder();
            if (convertView == null) {
                convertView = View.inflate(Ch2_marketActivity.this, R.layout.activity_ch2_market_item, null);
                viewHolder.imageView = convertView.findViewById(R.id.ch2_market_item_image);
                viewHolder.textView = convertView.findViewById(R.id.ch2_market_tv_item);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.imageView.setImageResource(img[position]);
            viewHolder.textView.setText(names[position]);
            return convertView;
        }
    }

    class ViewHolder {
        ImageView imageView;
        TextView textView;

    }
}

